package net.zjitc.controller;

import net.zjitc.domain.Result;
import net.zjitc.domain.User;
import net.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
private UserService userService;
    @PostMapping
    public Boolean add(@RequestBody User user){
        return userService.addUser(user);

    }
    @DeleteMapping("/{id}")
    public Boolean deleteById(@PathVariable("id")Integer id){
        try{
            userService.deleteById(id);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    @GetMapping("/{id}")
    public User findById(@PathVariable("id")Integer id){
        User user;
        try {
            user = userService.findById(id);
        } catch (Exception e) {
            return null;
        }
        return user;
    }
    @PutMapping
    public Boolean update(@RequestBody User user){
        try {
            userService.update(user);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    @PostMapping("/{currentPage}/{pageSize}")
    public Result getPage(@PathVariable("currentPage") int currentPage,
                          @PathVariable("pageSize") int pageSize,
                          @RequestBody User user) {
        return userService.findPage(currentPage,pageSize,user);
    }
}
